package memdb

import (
	SqliteDB "webSer/sqlitedb"

	us "gitee.com/gouyabin/go_utils"
)

var Log *us.Logs

type Auth struct {
	AuthStr string
	OutTime int64
	Write   bool
}

var AuthDB []Auth

//验证权限
func CheckAuth(a string) (int, Auth) {
	//判断内存是否有Auth缓存。
	for _, value := range AuthDB {
		if value.AuthStr == a {
			return 0, value
		}
	}
	ut := SqliteDB.User{}
	//数据库中查找是否有auth，并判断user状态
	if Log == nil {
		Log = us.Log
	}
	Log.Debug("CheckAuth,a", a)
	codeNum, user := ut.CheckAuth(a)
	//如果状态不是0，说明账户有问题
	if codeNum != 0 {
		// us.WriteLogV2("MemDB-CheckAuth-用户权限验证错误",)
		return codeNum, Auth{}
	}
	userGroup := SqliteDB.Group{}
	AuthSt := Auth{
		AuthStr: a,
		OutTime: user.AuthOverTime,
		Write:   userGroup.UserIDFindConfig(user.Id), //获取是否读写
	}
	AuthDB = append(AuthDB, AuthSt)
	return 0, AuthSt
}
